package com.hay.upload.controller;

import com.hay.upload.service.UploadService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

/**
 * @title: UploadController
 * @Author HuangYan
 * @Date: 2020/7/21 19:36
 * @Version 1.0
 */
@Controller
@RequestMapping("upload")
public class UploadController {

    @Autowired
    UploadService uploadService;

    @PostMapping("image")
    public ResponseEntity<String> uploadImages(@RequestParam("file")MultipartFile file){

        String url = uploadService.uploadImages(file);
        if (StringUtils.isBlank(url)){
            ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.status(HttpStatus.CREATED).body(url);
    }
}
